//
//  ViewController.m
//  TASWorkHelper
//
//  Created by 于桂成 on 2018/8/28.
//  Copyright © 2018年 并联科技. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property(nonatomic, strong) UIImageView * myImageView;
@property(nonatomic, strong) UIButton * addPhotoBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 400)];
    self.myImageView.backgroundColor = YXGlobalColor;

    [self.view addSubview:self.myImageView];
    
    self.addPhotoBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 450, 200, 40)];
    [self.addPhotoBtn addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.addPhotoBtn setTitle:@"上传工作日志" forState:UIControlStateNormal];
    [self.addPhotoBtn setTitleColor:YXGlobalColor forState:UIControlStateNormal];

    [self.view addSubview:self.addPhotoBtn];
}

-(void)selectPhoto {
    YXLog(@"选择图片");
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"选择上传方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openAlbum];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openCamera];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
    
}

-(void)openAlbum {
    UIImagePickerController* pickVC = [[UIImagePickerController alloc]init];
    pickVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pickVC.delegate = self;
    [self presentViewController:pickVC animated:YES completion:^{
        
    }];
}
-(void)openCamera {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        YXLog(@"不可用");
        return;
    }
    UIImagePickerController* pickVC = [[UIImagePickerController alloc]init];
    pickVC.sourceType = UIImagePickerControllerSourceTypeCamera;
    pickVC.delegate = self;
    [self presentViewController:pickVC animated:YES completion:^{
        
    }];
}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    YXLog(@"%@",info);
    self.myImageView.image = info[UIImagePickerControllerOriginalImage];

    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
